<?php
class ArticleAction extends AdminAction {	

	/*
	 *新闻部分的增删改操作
	 */

	#文章列表
	function index() {

		$cate = M('Category')->getField('id,name');
		$path = I('get.path');
		$key  = I('get.key');

		$article = M('Article');
		if($path!=''){
			$map['path'] = array('like', $path.'%');
		}

		if($key!=''){
			$map['title'] = array('like', '%'.$key.'%');
		}

		if ($this->adminGroup !='超级管理员') {
			$map['cid'] = array('in',$this->arr);
		}

		$count = $article->where($map)->count();

		import("@.ORG.Page");

		$page = new Page($count, 20);

		$show = $page->show();

		$list = $article->where($map)->order('sort asc , id desc')->limit($page->firstRow . ',' . $page->listRows)->select();

		$artClass = M('Category');

		unset($map);
		if ($this->adminGroup !='超级管理员') {			
			$map['id'] = array('in',$this->arr);
		}

		$sclass = $artClass->field("id,name,fid,path")->where($map)->order('path')->select();


		foreach ($sclass as $key => $value) {

			$count = count(explode('-', $value['path'])) - 3;

			$sclass[$key]['count'] = $count;

		}

		$this->assign('sclass', $sclass);
		$this->assign('list', $list);
		$this->assign('cate', $cate);
		$this->assign('page', $show);
		$this->assign('path', $path);

		$this->display();

	}	

	#文章添加
	function Add() {		

		$path = $_GET['path'];

		$artClass = M('Category');

		if ($this->adminGroup !='超级管理员') {			
			$map['id'] = array('in',$this->arr);
		}

		$list = $artClass->field("id,name,fid,path")->where($map)->order('path')->select();		

		foreach ($list as $key => $value) {

			$count = count(explode('-', $value['path'])) - 3;

			$list[$key]['count'] = $count;
		}

		$this->assign('list', $list);

		$this->assign('path', $path);

		$this->display();

	}

	#文章保存
	function Save() {	

		$article = D('Article');
		if ($date = $article->create()) {

			if ($article->add($date)) {
				$state = 'SUCCESS';
                $message = '文章添加成功';
                $url = U('Article/index');
			} else {
				$state = '文章添加失败';
			}
		} else {
			$state = $article->getError();
		}
		echo $this->echo_json_str($state,$message,$url);
	}

	#文章编辑
	function Edit() {

		$id = (int) $_GET['id'];

		if (!isset ($id)) {
			$this->error('参数错误');
		}

		$artClass = M('Category');

		if ($this->adminGroup !='超级管理员') {			
			$map['id'] = array('in',$this->arr);
		}

		$clist = $artClass->field("id,name,fid,path")->where($map)->order('path')->select();

		foreach ($clist as $key => $value) {

			$count = count(explode('-', $value['path'])) - 3;

			$clist[$key]['count'] = $count;

		}

		$article = M('Article');

		$list = $article->where("id=$id")->find();

		if (!$list) {
			$this->error('信息不存在');
		} else {
			if ($list['picname']=='') {
				$list['pic'] = STATICS.'/image/image.jpg';
			}else{
				$list['pic'] = $list['picname'];
			}
			$this->assign('list', $list);
			$this->assign('clist', $clist);

			$this->display();
		}

	}

	#更新文章
	function Update() {

		$article = D('Article');

		if ($date = $article->create()) {

			if ($article->save($date)) {
				$state = 'SUCCESS';
                $message = '文章编辑成功';
                $url = U('Article/index');
			} else {
				$state = '文章编辑失败';
			}
		} else {
			$state = $article->getError();
		}
		echo $this->echo_json_str($state,$message,$url);

	}

	#文章删除
	function Del() {

		$id=I('post.selectedids');
		$cityID = I('post.city');

		if($id==''){
			$state = "您没有选择任何信息！";
		}else{
			foreach ($id as $v) {
				$article = M('Article');
				$where['id'] = $v;
				$list = $article->field('picname')->where($where)->find();
				if ($list['picname']!='') {
					unlink(C('SITE_PATH').$list['picname']);
				}
				$article->where($where)->delete();
			}
			$state = 'SUCCESS';
            $message = '文章删除成功';
            $url = U('Article/index');
		}
		echo $this->echo_json_str($state,$message,$url);
	}
}
?>